import 'package:flutter/material.dart';
import 'package:xvideos/hj_model/mine/happy/HappyModel.dart';
import 'package:xvideos/hj_model/mine/happy/ShuApp.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/banner/ads_banner_widget.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:url_launcher/url_launcher.dart';

class ADListSubView extends StatefulWidget {
  final ADRenderConfigModel model;
  const ADListSubView(this.model, {super.key});

  @override
  State<ADListSubView> createState() => _ADListSubViewState();
}

class _ADListSubViewState extends State<ADListSubView> {
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        if (widget.model.bannerAds?.isNotEmpty ?? false)
          SliverPadding(
            padding: EdgeInsets.only(left: 16, right: 16, top: 12),
            sliver: SliverToBoxAdapter(
              child: ClipRRect(
                borderRadius: BorderRadius.circular(8),
                child: AspectRatio(
                  aspectRatio: 343 / 143,
                  child: AdsBannerWidget(
                    widget.model.advsToAdsInfoBeans(),
                    onItemClick: (index) {
                      final model = widget.model.advsToAdsInfoBeans()[index];
                      MineService.recreationList(model.id ?? "", "adv");

                      launchUrl(Uri.parse(model.href ?? ''));
                    },
                  ),
                ),
              ),
            ),
          ),
        if (widget.model.hengAds != null)
          SliverMainAxisGroup(
            slivers: [
              SliverToBoxAdapter(
                child: _buildNormalTitle(widget.model.hengAds?.title ?? ''),
              ),
              SliverToBoxAdapter(
                child: SizedBox(height: 10),
              ),
              SliverPadding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                sliver: SliverGrid.builder(
                    itemCount: widget.model.hengAds?.shuApps?.length,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 5,
                      crossAxisSpacing: 2,
                      mainAxisSpacing: 10,
                      childAspectRatio: 82 / 101,
                    ),
                    itemBuilder: (_, index) => _buildVItem(widget.model.hengAds!.shuApps![index])),
              )
            ],
          ),
        if (widget.model.shuAds != null)
          SliverMainAxisGroup(
            slivers: [
              SliverToBoxAdapter(
                child: _buildNormalTitle(widget.model.shuAds?.title ?? ''),
              ),
              SliverPadding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                sliver: SliverList.separated(
                  itemCount: widget.model.shuAds?.shuApps?.length ?? 0,
                  separatorBuilder: (_, __) => widget.model.shuAds?.title == '热门推荐' ? 10.sizeBoxH : 1.line,
                  itemBuilder: (_, index) {
                    final model = widget.model.shuAds!.shuApps![index];
                    if (widget.model.shuAds?.title == '热门推荐') return _buildHBItem(model);
                    return _buildHItem(model);
                  },
                ),
              )
            ],
          ),
      ],
    );
  }

  Widget _buildNormalTitle(String title) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
      child: Row(
        children: [
          SizedBox(width: 15),
          Text(
            title,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w600,
              fontSize: 16.0,
            ),
          ),
        ],
      ),
    );
  }

  /// 竖版的广告
  Widget _buildVItem(ShuApp item) {
    return InkWell(
      onTap: () {
        MineService.recreationList(item.id ?? "", "adv");

        launchUrl(Uri.parse(item.url ?? ''));
      },
      child: Container(
        height: 100,
        child: Column(
          children: [
            Expanded(
              child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(0)),
                child: NetworkImageLoader(
                  imageUrl: item.icon ?? '',
                  // width: double.infinity,
                  height: double.infinity,
                  fit: BoxFit.fitHeight,
                  borderRadius: 12,
                ),
              ),
            ),
            SizedBox(height: 6),
            Text("${item.name}",
                maxLines: 1,
                style: const TextStyle(
                  color: const Color(0xffdddfdf),
                  fontWeight: FontWeight.w300,
                  fontSize: 12.0,
                ),
                textAlign: TextAlign.center)
          ],
        ),
      ),
    );
  }

  Widget _buildHItem(ShuApp item) {
    return InkWell(
      onTap: (){
        MineService.recreationList(item.id ?? "", "adv");
        launchUrl(Uri.parse(item.url ?? ''));
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(
            height: 12,
          ),
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(4)),
                      child: NetworkImageLoader(
                        imageUrl: item.icon ?? '',
                        borderRadius: 12,
                        width: (MediaQuery.of(context).size.width - 90)/5,
                        height: (MediaQuery.of(context).size.width - 90)/5,
                      ),
                    ),
                    SizedBox(width: 10),
                    Column(
                      children: [
                        SizedBox(
                          width: MediaQuery.of(context).size.width - 80 - 60 - 50,
                          child: Text("${item.name}",
                            style: const TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.w700,
                              fontSize: 14,
                            ),
                            textAlign: TextAlign.start,
                          ),
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width - 80 - 60 - 50,
                          child: Text("${item.desc}",
                            style: const TextStyle(
                              color: Color(0xFF989898),
                              fontWeight: FontWeight.w300,
                              fontSize: 12.0,
                            ),
                            overflow: TextOverflow.ellipsis,  // 超出部分显示省略号
                            maxLines: 2,  // 限制为单行
                            textAlign: TextAlign.start,
                          ),
                        ),
                      ],
                    )
                  ],
                ),
                InkWell(
                  onTap: () {
                    MineService.recreationList(item.id ?? "", "adv");
                    launchUrl(Uri.parse(item.url ?? ''));
                  },
                  child: Container(
                    width: 72,
                    height: 28,
                    decoration: BoxDecoration(
                      color: Color(0xffE1351F),
                      borderRadius: BorderRadius.all(Radius.circular(6)),
                    ),
                    child: Center(
                      child: Text(
                        '立即下载',
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
          SizedBox(
            height: 12,
          ),
          // Container(color: Color(0xff333333), height: 1)
        ],
      ),
    );
  }

  Widget _buildHBItem(ShuApp item) {
    return InkWell(
      onTap: () {
        MineService.recreationList(item.id ?? "", "adv");
        launchUrl(Uri.parse(item.url ?? ''));
      },
      child: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AspectRatio(
              aspectRatio: 72 / 20,
              child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(4)),
                child: NetworkImageLoader(
                  imageUrl: item.icon ?? '',
                  width: double.infinity,
                  height: double.infinity,
                ),
              ),
            ),
            SizedBox(height: 10),
            Text(
              "${item.name}",
              style: const TextStyle(
                color: const Color(0xffdddfdf),
                fontWeight: FontWeight.w300,
                fontSize: 12.0,
              ),
            ),
            SizedBox(height: 10),
          ],
        ),
      ),
    );
  }
}
